<?php

class Public_bookings extends Controller {

	function Public_bookings()
	{
		parent::Controller();
		$this->load->helper(array('url', 'date', 'dropdownlist'));	
		$this->load->library('session');
	}
	
	function index()
	{
		$this->load->view('public_bookings/index');
	}
	
	function search_availability_widget()
	{
		$this->load->model('Tour_model', '', TRUE);
		$data = array();
		$data['results'] = $this->Tour_model->get_tours_for_booking($_POST);
		$data['passengers'] = $_POST['booking_passengers'];
		$data['admin'] = $_POST['admin'];
		$this->load->view('public_bookings/tour_availability', $data);
	}	
	
	function step1() //pick a tour
	{

		$this->load->helper('bookings');
		$this->load->model('Booking_model', '', TRUE);
		$this->load->model('Tourtype_model', '', TRUE);
	
		$this->load->library('form_validation');
		$this->form_validation->set_rules('booking_tour', 'Tour', 'required');			
		$this->form_validation->set_rules('booking_passengers', 'Number of passengers', 'required|numeric');	
		$data = array();
		
		$data['tourtypes'] = $this->Tourtype_model->get_all_tourtypes();
		
		if(!isset($_POST['go_to_step2'])) //when the user arrived on the page
		
			$this->load->view('public_bookings/step1',$data);		
		
		else if (isset($_POST['go_to_step2']) && $this->form_validation->run() == TRUE) //if all right
		{
		  $newdata = array(
                   'booking_tour'  => $_POST['booking_tour'],
                   'booking_passengers'     => $_POST['booking_passengers']
               );
		  
		  $this->session->set_userdata($newdata);
		  redirect(site_url("public_bookings/step2"));
		} else
		{
			$this->load->view('public_bookings/step1',$data); //if the form is not complete
		}
	}
	
	
	function step2() //user details
	{
		//if the user logs in before starting a booking
		if( ($this->session->userdata('user_logged_in')) &&  (!$this->session->userdata('booking_first_name')) )
		{
			//set the "booking_var" from the user_logged_in if the user is logged in
			$user = $this->session->userdata('user_logged_in');
			$this->session->set_userdata('booking_first_name',$user['first_name']);
			$this->session->set_userdata('booking_last_name',$user['last_name']);
			$this->session->set_userdata('booking_sex', $user['sex']);
			$this->session->set_userdata('booking_nationality', $user['nationality']);
			$this->session->set_userdata('booking_email',$user['e_mail']);
			$this->session->set_userdata('booking_phone',$user['phone_number']);
			$this->session->set_userdata('booking_country',$user['country']);
			$this->session->set_userdata('booking_city',$user['city']);
			$this->session->set_userdata('booking_post_code',$user['post_code']);
			$this->session->set_userdata('booking_address',$user['address']);			
		}
		
	    //check if the datas of the previous step are loaded in the session
		$test = $this->session->userdata('booking_tour');
		if(!empty($test))
		{		
			$this->load->library('form_validation');
			if (!$this->session->userdata('user_logged_in'))
			{
				$this->form_validation->set_rules('booking_first_name', 'First Name', 'trim|required');
				$this->form_validation->set_rules('booking_last_name', 'Last Name', 'trim|required');
				$this->form_validation->set_rules('booking_sex', 'Sex', 'trim|required');
				$this->form_validation->set_rules('booking_nationality', 'Nationality', 'trim|required');
				$this->form_validation->set_rules('booking_country', 'Country', 'trim|required');
				$this->form_validation->set_rules('booking_email', 'E-mail address', 'trim|required|valid_email');
				$this->form_validation->set_rules('booking_phone', 'Telephone number', 'trim|required|check_phone');
				$this->form_validation->set_rules('booking_city', 'City', 'trim');
				$this->form_validation->set_rules('booking_post_code', 'Post Code', 'trim');
				$this->form_validation->set_rules('booking_address', 'Address', 'trim');	
			}

			$data['links_list'] = 	array(
										array('link' => 'step1', 'value' => "Step 1"),
									);			
			
			if(!isset($_POST['go_to_step3'])) //when the user arrived on the page
				$this->load->view('public_bookings/step2',$data);		
			else if (isset($_POST['go_to_step3']) && $this->form_validation->run() == TRUE) //if all right
			{
				$password =substr( md5( time() . rand() . $_POST['booking_email'] ), 0, 12 );
				$marketing = (isset($_POST['booking_marketing']) && $_POST['booking_marketing'] == 1) ? 1 : 0;
				$newdata = array(
					   'booking_first_name'  => $_POST['booking_first_name'],
					   'booking_last_name'     => $_POST['booking_last_name'],
					   'booking_sex'     => $_POST['booking_sex'],
					   'booking_nationality'  => $_POST['booking_nationality'],
					   'booking_email'  => $_POST['booking_email'],
					   'booking_phone'     => $_POST['booking_phone'],
					   'booking_country'     => $_POST['booking_country'],
					   'booking_city'     => $_POST['booking_city'],
					   'booking_post_code'  => $_POST['booking_post_code'],
					   'booking_address'     => $_POST['booking_address'],					   
					   'booking_marketing'     => $marketing,	
					   'booking_password'	=> $password				   
					   );
					   
					   
				//check if the user already exists
				$this->load->model('User_model', '', TRUE);
				if($this->User_model->user_exists($_POST['booking_email']))
				{
					redirect(site_url("public_bookings/check_user"));
				}
				else
				{
					$this->session->set_userdata($newdata);
					redirect(site_url("public_bookings/step3"));
			  }					   
			  
			}		
			elseif(isset($_POST['go_to_step3']) && $this->session->userdata('user_logged_in'))
			{
				redirect(site_url("public_bookings/step3"));
			}
			else
			{			
				$this->load->view('public_bookings/step2',$data);		//if the form is not complete
			}
		}
		else //no var in the session -> back to the step one (pick a tour)
			redirect(site_url("public_bookings/step1"));
	}
	
	function step3()  //passengers details
	{

		if( ($this->session->userdata('user_logged_in')) &&  (!$this->session->userdata('booking_first_name')) )
		{
			//set the "booking_var" from the user_logged_in datas
			$user = $this->session->userdata('user_logged_in');
			$this->session->set_userdata('booking_first_name',$user['first_name']);
			$this->session->set_userdata('booking_last_name',$user['last_name']);
			$this->session->set_userdata('booking_sex', $user['sex']);
			$this->session->set_userdata('booking_nationality', $user['nationality']);
			$this->session->set_userdata('booking_email',$user['e_mail']);
			$this->session->set_userdata('booking_phone',$user['phone_number']);
			$this->session->set_userdata('booking_country',$user['country']);
			$this->session->set_userdata('booking_city',$user['city']);
			$this->session->set_userdata('booking_post_code',$user['post_code']);
			$this->session->set_userdata('booking_address',$user['address']);			
		}
		
		//check if the datas of the previous step are loaded in the session
		$first_name = $this->session->userdata('booking_first_name'); // used if the user is not logged in
		if(!empty($first_name))
		{	
			$this->load->library('form_validation');	
			for($i = 0; $i < $this->session->userdata('booking_passengers'); $i++)
			{
				$this->form_validation->set_rules('booking_passenger_first_name['.$i.']', 'First Name', 'trim|required');
				$this->form_validation->set_rules('booking_passenger_last_name['.$i.']', 'Last Name', 'trim|required');
				$this->form_validation->set_rules('booking_passenger_sex['.$i.']', 'Sex', 'trim|required');
				$this->form_validation->set_rules('booking_passenger_nationality['.$i.']', 'Nationality', 'trim|required');
				if($i == 0) $this->form_validation->set_rules('booking_passenger_emergency['.$i.']', 'Emergency phone number', 'trim|required|check_phone');				
				else $this->form_validation->set_rules('booking_passenger_emergency['.$i.']', 'Emergency phone number', 'trim|check_phone');								
					
			}

			$data = array(
				'booking_first_name'  => $first_name,
				'booking_last_name'  => $this->session->userdata('booking_last_name'),
				'booking_sex'     => $this->session->userdata('booking_sex'),
				'booking_number_passengers'     => $this->session->userdata('booking_passengers'),
				'booking_nationality'  => $this->session->userdata('booking_nationality'),
				'booking_email'  => $this->session->userdata('booking_email'),
				'booking_phone'     => $this->session->userdata('booking_phone'),
				'booking_country'     => $this->session->userdata('booking_country'),
				'booking_city'     => $this->session->userdata('booking_city'),
				'booking_post_code'  => $this->session->userdata('booking_post_code'),
				'booking_address'     => $this->session->userdata('booking_address')					
			);


			$data['links_list'] = 	array(
							array('link' => 'step1', 'value' => "Step 1"),
							array('link' => 'step2', 'value' => "Step 2"),							
						);

			if(!isset($_POST['go_to_step4'])) //when the user arrived on the page
				$this->load->view('public_bookings/step3',$data);
			else if (isset($_POST['go_to_step4']) && $this->form_validation->run() == TRUE) //if all right
			{
   			   $all_passengers = array();
			   	for($i = 0; $i < $this->session->userdata('booking_passengers'); $i++)
				{
				   $all_passengers[$i] = array(
					   'first_name'  => $_POST['booking_passenger_first_name'][$i],
					   'last_name'     => $_POST['booking_passenger_last_name'][$i],
					   'sex'     => $_POST['booking_passenger_sex'][$i],
					   'nationality'     => $_POST['booking_passenger_nationality'][$i],					   
					   'emergency_phone_number'     => $_POST['booking_passenger_emergency'][$i]
				   );
			   }
			  
			  $this->session->set_userdata('passengers',$all_passengers);
			  redirect(site_url("public_bookings/step4"));
			}
			else
				$this->load->view('public_bookings/step3',$data);
		}
		else //no var in the session -> back to the step two (user details)
			redirect(site_url("public_bookings/step2"));		
	}
	
	function step4() //confirmation
	{
	    //check if the datas of the previous step are loaded in the session
		$passengers = $this->session->userdata('passengers');
		if(!empty($passengers))
		{	
			$tour_id = $this->session->userdata('booking_tour');
			
			$this->load->library('form_validation');	
			$this->form_validation->set_rules('booking_terms', 'Accept', 'required');
			$this->form_validation->set_rules('booking_promo_code', 'Promotion code', 'trim');			
			$this->form_validation->set_message('required', 'You have to accept the Terms an Conditions to continue');
			
			$this->load->model('Tour_model', '', TRUE);
		    $tourinfo = $this->Tour_model->get_tour_info($tour_id);

    	    $data = array (
					'booking_tour' => $tourinfo,
					'booking_first_name'  => $this->session->userdata('booking_first_name'),
					'booking_last_name'  => $this->session->userdata('booking_last_name'),
					'booking_sex'     => $this->session->userdata('booking_sex'),					
					'booking_nationality'  => $this->session->userdata('booking_nationality'),
					'booking_email'  => $this->session->userdata('booking_email'),
					'booking_phone'     => $this->session->userdata('booking_phone'),
					'booking_country'     => $this->session->userdata('booking_country'),
					'booking_city'     => $this->session->userdata('booking_city'),
					'booking_post_code'  => $this->session->userdata('booking_post_code'),
					'booking_address'     => $this->session->userdata('booking_address'),
					'booking_passengers'     => $this->session->userdata('passengers')					
			 );
			$data['links_list'] = 	array(
									array('link' => 'step1', 'value' => "Step 1"),
									array('link' => 'step2', 'value' => "Step 2"),							
									array('link' => 'step3', 'value' => "Step 3"),							
								);	

			if(isset($_POST['booking_promo_code']) && (!empty($_POST['booking_promo_code'])) ) 
			{
				$price_off = 3.70;
				$data['price_off'] = $price_off;
			}					
			
			if(isset($_POST['confirm']) &&  $this->form_validation->run() == TRUE) 
			{
				$this->session->set_userdata('booking_total',$_POST['booking_total']);
				redirect(site_url("public_bookings/step5"));	
			}
			else
				$this->load->view('public_bookings/step4',$data);
		}
		else
			redirect(site_url("public_bookings/step3"));		
	}


	function step5() 
	{
		$total = $this->session->userdata('booking_total');
		if(!empty($total))
		{
			if(!$this->session->userdata('booking_inserted')) // in order to not insert the booking twice
			{
				//user insertion
				$this->load->model('User_model', '', TRUE);
				$data_user = array (
					'first_name'  => $this->session->userdata('booking_first_name'),
					'last_name'  => $this->session->userdata('booking_last_name'),
					'sex'     => $this->session->userdata('booking_sex'),					
					'nationality'  => $this->session->userdata('booking_nationality'),
					'e_mail'  => $this->session->userdata('booking_email'),
					'phone_number'     => $this->session->userdata('booking_phone'),
					'country'     => $this->session->userdata('booking_country'),
					'city'     => $this->session->userdata('booking_city'),
					'post_code'  => $this->session->userdata('booking_post_code'),
					'address'     => $this->session->userdata('booking_address'),
					'marketing' => $this->session->userdata('booking_marketing'),
					'password' => $this->session->userdata('booking_password')
				);
				if(!$this->session->userdata('user_logged_in'))
					$user_id = $this->User_model->insert_user($data_user);
				else
				{
					$user = $this->session->userdata('user_logged_in');
					$user_id = $user['user_id'];
				}
				 

				//booking insertion
				$this->load->model('Booking_model', '', TRUE);
				$this->load->model('Tour_model', '', TRUE);
				$date = date("Y-m-d H:m:s");
				$data_booking = array (
					'tour_id' => $this->session->userdata('booking_tour'),
					'user_id' => $user_id,
					'booking_date' => $date
				);		
				$id_booking = $this->Booking_model->insert_booking($data_booking,$this->session->userdata('booking_passengers'));
				//generate the booking_number Macbs_TourCode_DepartureDate_nbPassengers_bookingID
				$tour_id = $this->session->userdata('booking_tour');
				$passengers_number = $this->session->userdata('booking_passengers');
				$tour_code = $this->Tour_model->get_tourcode($tour_id);
				$departure_date = $this->Tour_model->get_departure($tour_id);
				$booking_number = "Macbc_".$tour_code."_".$departure_date."_".$passengers_number."_".$id_booking;
				$new_data = array('booking_number' => $booking_number);
				$this->Booking_model->update_booking($id_booking, $new_data);
				
				//passengers insertion
				$this->load->model('Passenger_model', '', TRUE);
				$passengers = $this->session->userdata('passengers');
				foreach($passengers as $passenger)
				{
				   $passenger['booking_id'] = $id_booking;
				   $this->Passenger_model->insert_passenger($passenger);
				}				
				
				//payment insertion
				$this->load->model('Payment_model', '', TRUE);
				$payment_auth = md5( time() . rand() . $data_user['e_mail'] );
				$data_payment = array(
								'booking_id' => $id_booking,
								'amount' => $this->session->userdata('booking_total'),
								'method' => "Credit Card",
								'location' => "PROTX",
								'payment_auth' => $payment_auth,
								'payment_date' => date("Y-m-d H:m:s")
				);
				$payment_id = $this->Payment_model->insert_payment($data_payment);				

				
				$data = array(
						'payment_id' => $payment_id,
						'payment_auth' => $payment_auth 
						);
						
				$data['links_list'] = 	array(
									array('link' => 'step1', 'value' => "Step 1"),
									array('link' => 'step2', 'value' => "Step 2"),							
									array('link' => 'step3', 'value' => "Step 3"),							
									array('link' => 'step4', 'value' => "Step 4"),																
								);							
						
				$this->load->view("public_bookings/step5",$data);
			}	
		}
		else
			redirect(site_url("public_bookings/step4"));			
	}
	
	function step6($payment_id, $payment_auth){ //used for the online payment
		$this->load->model('Payment_model', '', TRUE);
		$test = $this->Payment_model->check_auth($payment_id, $payment_auth);
		if($test)
		{
			$this->Payment_model->update_payment($payment_id, array('payment_receive' => 1));
			$data['msg'] = "Authentication successful  !";
		}
		else 	
		{
			$data['msg'] = "Authentication failed !";
		}
		$data['links_list'] = 	array(
					array('link' => 'step1', 'value' => "Step 1"),
					array('link' => 'step2', 'value' => "Step 2"),							
					array('link' => 'step3', 'value' => "Step 3"),							
					array('link' => 'step4', 'value' => "Step 4"),																
					array('link' => 'step5', 'value' => "Step 5"),																					
				);		
		$this->load->view("public_bookings/step6",$data);
	}
	
	
	function check_user() 
	{
		
		$this->load->library('form_validation');
		$this->form_validation->set_rules('login_mail', 'E-mail Address', 'trim|required|valid_email');
		$this->form_validation->set_rules('login_password', 'Password', 'trim|required');
		
		$this->session->set_flashdata('link_redirect', site_url("public_bookings/step3"));
		$this->load->view('public_bookings/check_user');
	}
	
}
?>
